<template>
  <div class="article-comments">
    <!-- 评论列表 -->
    <van-list
      v-model="loading"
      :finished="finished"
      finished-text="没有更多了"
      @load="onLoad"
    >
      <van-cell
        v-for="(item) in list"
        :key="item.com_id"
      >
        <van-image
          slot="icon"
          round
          width="30"
          height="30"
          style="margin-right: 10px;"
          :src="item.aut_photo"
        />
        <span slot="title" style="color: #466b9d;">{{ item.aut_name }}</span>
        <div slot="label">
          <p style="color: #363636;">{{ item.content }}</p>
          <p>
            <span style="margin-right: 10px;">{{ item.pubdate |relative }}</span>
            <van-button size="mini" type="default" @click="addReplay(item)">回复</van-button>
          </p>
        </div>
        <van-icon slot="right-icon" :name="item.is_liking?'like':'like-o'" @click="isLikeComment(item)" />
      </van-cell>
    </van-list>
    <!-- 评论列表 -->

    <!-- 发布评论 -->
    <van-cell-group class="publish-wrap">
      <van-field
        v-model="content"
        clearable
        placeholder="请输入评论内容"
      >
        <van-button slot="button" size="mini" type="info" @click="clickAddComment">发布</van-button>
      </van-field>
    </van-cell-group>
    <!-- /发布评论 -->
    <van-popup v-model="show" round position="bottom" :style="{ height: '85%' }"><CommentReply v-if="show" :comment="comment" :article-id="articleId" @close="show=false" /></van-popup>
  </div>
</template>

<script>
import CommentReply from '@/views/article/comment/commentReplay.vue'
import { reqCommentLike, reqGetArticleComments, reqDelCommentLike, reqAddComment } from '@/api/comments'
export default {
  name: 'ArticleComment',
  components: {
    CommentReply
  },
  props: {
    articleId: {
      type: String,
      required: true
    }
  },
  data() {
    return {
      list: [], // 评论列表
      loading: false, // 上拉加载更多的 loading
      finished: false, // 是否加载结束
      offset: null,
      content: '',
      show: false,
      comment: ''
    }
  },

  methods: {
    async onLoad() {
      const { data } = await reqGetArticleComments(this.articleId, this.offset)
      this.offset = data.last_id
      // console.log(data)
      this.list.push(...data.results)
      this.loading = false
      if (data.results.length === 0) {
        this.finished = true
      }
    },
    async isLikeComment(item) {
      if (item.is_liking) {
        await reqDelCommentLike(item.com_id)
        this.$toast.success('取消点赞成功')
      } else {
        await reqCommentLike(item.com_id)
        this.$toast.success('点赞成功')
      }
      item.is_liking = !item.is_liking
    },
    // 发布评论===============
    async clickAddComment() {
      if (this.content.trim() === '') return
      const { data: { new_obj }} = await reqAddComment(this.articleId, this.content)
      this.$toast.success('发布成功')
      // console.log(new_obj)
      this.content = ''
      this.list.unshift(new_obj)
    },
    addReplay(id) {
      this.show = true
      this.comment = id
    }
  }
}
</script>

<style scoped lang='scss'>
// 发表评论的区域是固定在下端的
.publish-wrap {
  position: fixed;
  left: 0;
  bottom: 0;
  width: 100%;
}
// 给发表评论区空出地方
.van-list {
  margin-bottom: 45px;
}
</style>
